<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/static/common/default.jsp"%>
<%
	String openId = "";
	if(request.getAttribute("openID")!=null){
		openId = request.getAttribute("openID").toString();
	}
%>
<!DOCTYPE html>
<html lang="en">
	 <head>
	 <jsp:include page="/style/header.jsp"></jsp:include>
	 </head>
	
	<body>
	
	<div class="title_bar">
        <h3>用户注册</h3>
        <!--<a href="javascript:history.go(-1);" class="go_back"></a>  -->
    </div>
    <div class="register">
        <div class="ipt_list">
        
        	<div class="ipt_cell">
                <span class="tag">账号</span>
                <input type="text" placeholder="请输入账号" name="loginName" id="loginName">
            </div>
            <div class="ipt_cell">
                <span class="tag">姓名</span>
                <input type="text" placeholder="请输入姓名" id="username">
            </div>
            <div class="ipt_cell">
                <span class="tag">电子邮箱</span>
                <input type="text" placeholder="请输入电子邮箱" id="email">
            </div>
            <div class="ipt_cell">
                <span class="tag">手机号码</span>
                <input type="text" placeholder="请输入手机号码" id="phone">
            </div>
            <div class="ipt_cell">
                <span class="tag">性别</span>
                <input type="text" class="slt" placeholder="请选择性别" id="sex">
            </div>
            <div class="ipt_cell">
                <span class="tag">密码</span>
                <input type="password" placeholder="请输入密码" id="password">
            </div>
            <div class="ipt_cell">
                <span class="tag">确认密码</span>
                <input type="password" placeholder="请输入确认密码" id="repassword">
            </div>
        </div>
        <div class="ipt_list">
            <div class="ipt_cell">
                <span class="tag">项目</span>
                <input type="text" class="slt" placeholder="请输入项目" id="project">
            </div>
            <div class="ipt_cell">
                <span class="tag">工段</span>
                <input type="text" class="slt" placeholder="请输入工段" id="work_section">
            </div>
            <div class="ipt_cell">
                <span class="tag">职务</span>
                <input type="text" placeholder="请输入职务" id="job">
            </div>
            <div class="ipt_cell verify_box" style="display: none;">
                <span class="tag">验证码</span>
                <input type="text" placeholder="验证码" id="verify_code" value="8888">
                <div class="get_verify_code">获取验证码</div>
            </div>
        </div>
        <div class="btn" id="btn_reg">注 册</div>
        <input type="hidden" id="openid" name="openid" value="<%=openId%>"/>
        <input type="hidden" id="sourceid" name="sourceid" value="2"/>
    </div>
    <jsp:include page="/style/footer.jsp"></jsp:include>
    
    <script src="${ctx}/style/js/verify.js"></script>
    <script>
    
    // sex
    var picker_sex = new mui.PopPicker();
    picker_sex.setData([{
        value: "1",
        text: "男"
    }, {
        value: "2",
        text: "女"
    }])
    $('#sex').click(function() {
        picker_sex.show(function(SelectedItem) {
            $('#sex').val(SelectedItem[0].text);
            $('#sex').attr('data-value', SelectedItem[0].value);
        })
    });
    // project
    var picker_project = new mui.PopPicker();
    
    picker_project.setData(${projects});
    $('#project').click(function() {
        picker_project.show(function(SelectedItem) {
            $('#project').val(SelectedItem[0].text);
            $('#project').attr('data-value', SelectedItem[0].value);
            $.ajax({
    	        async: true,
    	        type:"POST",
    	        data:JSON.stringify({"projectID": SelectedItem[0].value}),
    	        
    	        url: "${ctx}/user/listSection?&mid="+Math.random(),
    	        dataType: "json",
    	        headers : {  
                    'Content-Type' : 'application/json;charset=utf-8'  
                },
                
                success : function(data) {
            		if (data != undefined && data.status != undefined
            				&& data.status.trim() == '0') {
            			picker_work_section.setData(data.data);
            			result = true;
            		} else {
            			result = false;
            			tiper(data.error);
            		}
            	},
    		},"json")
            
        })
    });
    // work_section
    var picker_work_section = new mui.PopPicker();
    $('#work_section').click(function() {
        picker_work_section.show(function(SelectedItem) {
            $('#work_section').val(SelectedItem[0].text);
            $('#work_section').attr('data-value', SelectedItem[0].value);
        })
    });
    $('#btn_reg').click(function(e) {
    	var loginName = $('#loginName').val();
        var username = $('#username').val();
        var email = $('#email').val();
        var phone = $('#phone').val();
        var sex = jQuery("#sex").data('value');
        var password = $('#password').val();
        var repassword = $('#repassword').val();
        var project = jQuery("#project").data('value');
        //var work_section = $('#work_section').val();
        var work_section = jQuery("#work_section").data('value');
        var job = $('#job').val();
        var verify_code = $('#verify_code').val();
        var openid = $('#openid').val();
        var sourceid = $('#sourceid').val();
        if(!loginName){
        	tiper('注册账号不能为空');
            return false;
        }else if(!vrf_name(loginName)){
        	tiper('账号为2-20位中文/大小写字母/下划线/数字/中划线');
            return false;
        }else if (!username) {
            tiper('姓名不能为空');
            return false;
        } else if (!vrf_name(username)) {
            tiper('姓名为2-20位中文/大小写字母/下划线/数字/中划线');
            return false;
        } else if (!email) {
            tiper('邮箱不能为空');
            return false;
        } else if (!vrf_email(email)) {
            tiper('邮箱格式不正确');
            return false;
        } else if (!phone) {
            tiper('手机号码不能为空');
            return false;
        } else if (!vrf_phone(phone)) {
            tiper('手机号码格式不正确');
            return false;
        } else if (!sex) {
            tiper('请选择性别');
            return false;
        } else if (!password) {
            tiper('密码不能为空');
            return false;
        } else if (!vrf_psw(password)) {
            tiper('密码为6-16位字母数字或特殊字符');
            return false;
        } else if (!repassword) {
            tiper('确认密码不能为空');
            return false;
        } else if (repassword != password) {
            tiper('两次输入密码不一致');
            return false;
        } else if (!project) {
            tiper('请选择项目');
            return false;
        } else if (!work_section) {
            tiper('请选择工段');
            return false;
        } else if (!job) {
            tiper('职务不能为空');
            return false;
        } else if (!verify_code) {
            tiper('验证码不能为空');
            return false;
        } else {
        	 var reg_url = "${ctx}/user/saveUser?&mid="+Math.random();
             var reg_jump_url = '';
             var reg_data = {
             	'loginName':loginName,
                 'userName': username,
                 'password': password, 
                 'email':email,
                 'phoneNum':phone,
                 'sex':sex,
                 'projectID':project,
                 'segmentID':work_section,
                 'postName':job,
                 'openId':openid,
                 'source':sourceid
             }
             myPost(reg_url,reg_data); 
        }
       
        
    })
    
    
    function myPost(reg_url,reg_data){
    	loading_show(); 
    	$.ajax({
	        async: true,
	        type:"POST",
	        data:JSON.stringify(reg_data),
	        url: "${ctx}/user/saveUser?&mid="+Math.random(),
	        dataType: "json",
	        headers : {  
                'Content-Type' : 'application/json;charset=utf-8'  
            },
            
            success : function(data) {
            	loading_hide(500); 
        		if (data != undefined && data.status != undefined
        				&& data.status.trim() == '0') {
        			result = true;
        			tiper('注册成功', 'green');
        			window.location.href = "${ctx}/user/toApprove";
        		} else {
        			result = false;
        			tiper(data.error);
        		}
        	},
		},"json")
    }
    </script>
	</body>
</html>